+function($){
  var deletedialog;
  var dialogContent = '<ul>'+
      '<input type="hidden" name="teid" id="teid">'+
      '<li><laber class="list-laber">客户姓名:</laber><input name="tename" id="tename" class="list-input"></li>'+
      '<li><laber class="list-laber">出生日期：</laber><div style="display:inline" class="YMD" data-ys="2017" data-ye="1880" id="tebirth"> <select class="YYYY" name="birth-YYYY"> <option value="">请选择 年</option> </select> <select class="MM" name="birth-MM"> <option value="">选择 月</option> </select> <select class="DD" name="birth-DD"> <option value="">选择 日</option> </select> </div></li>'+
      '<li><laber class="list-laber">品牌：</laber><span id="bid" class="J_selectbranditem"></span><a href="javascript:;" style="line-height:27px;" class="selectbrand">编辑</a></li>'+
      '<li><laber class="list-laber">店铺：</laber><span id="shopid" class="J_selectshopitem"></span><a href="javascript:;" style="line-height:27px;" class="selectshop">编辑</a></li>'+
      '<li><laber class="list-laber">收货人电话：</laber><input name="shopctel" id="tetel" class="list-input"></li>'+
      '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="terminalUpdate">修　改</button></div>'+
      '</ul>';
  $('body').on('click', '#Callback', function() {
    var teid = $(this).data('teid');
    var str = '<ul>'+
    '<li><laber class="list-laber">回访内容：</laber><textarea name="shopcallback" class="list-textarea" id="terminalcallback" rows="8" cols="38"></textarea></li>'+
    '<div class="dialog-button"><button class="btn btn-sm btn-primary" data-teid="'+teid+'" id="addCallback">添 加</button></div>'+
    '</ul>';
    var d = dialog({
      title:'添加回访',
      content: str,
      width:'450px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $('body').on('click', '#addCallback', function() {
    var teid = $(this).data('teid');
    if($('#terminalcallback').val()==""){
      alert('请先填写回访内容');
    }else{
      $.get("/terminal/addTerminalcallback",{
        teid:$(this).data('teid'),
        val:$('#terminalcallback').val()
      },function(data){
        if(data=="true"){
          location.reload(true);
        }
      });
    }
  });
  $('body').on('click', '.deleteTerminalCallback',function(){
    var content = '<span style="color:#d00">确定要删除这一条回访记录吗？</span>'+
                '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="callbackDelete" value="'+$(this).data('id')+'">确定</button><button style="margin-left:30px;" class="btn btn-sm btn-primary" id="closeDelete">取消</button></div>';
      deletedialog = dialog({
        title:'温馨提示',
        content: content,
        width:'300px',
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal();
   });
  $('body').on('click', '#closeDelete', function() {
     deletedialog.close();
   });
  $('body').on('click', '#callbackDelete', function() {
      $.post("/terminal/deleteTerminalcallback",{
        id:$(this).val()
      },function(data){
          location.reload(true);
      });
   });
  $('body').on('click', '#dialogUpdate', function() {
    var d = dialog({
      title:'修改店铺信息',
      content: dialogContent,
      width:'450px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal(dialoginit($(this)));
   });
   $('body').on('click', '#terminalUpdate', function() {
    // var postdata = $('#terminalupdateForm').serialize();
    var bid = '';
    for (var i = 0; i < window.brandcount.length; i++) {
      if(window.brandcount[i]){
        bid = bid+','+window.brandcount[i];
      }
    }
    bid = bid.replace(',','');
    var shopid = '';
    for (var i = 0; i < window.shopcount.length; i++) {
      if(window.shopcount[i]){
        shopid = shopid+','+window.shopcount[i];
      }
    }
    shopid = shopid.replace(',','');
    var shopname = '';
    for (var i = 0; i < window.shopnamecount.length; i++) {
      if(window.shopnamecount[i]){
        shopname = shopname+','+window.shopnamecount[i];
      }
    }
    shopname = shopname.replace(',','');
    var bname = '';
    for (var i = 0; i < window.brandnamecount.length; i++) {
      if(window.brandnamecount[i]){
        bname = bname+','+window.brandnamecount[i];
      }
    }
    bname = bname.replace(',','');
    var flag=true;
    $('.list-input,.list-textarea').each(function(index, el) {
      if($(this).val()==''){
        $(this).addClass('none');
        flag=false;
      }
    });
    if(flag==false){
      alert('请将信息补充完整');
      return false;
    }
    if(window.shopcount.length==0){
      alert('请选择店铺');
      $('.selectshop').css('border', '3px solid #f77');
      return false;
    }
    $('.selectshop').attr('style','');
    if(window.brandcount.length==0){
      alert('请选择品牌');
      $('.selectbrand').css('border', '3px solid #f77');
      return false;
    }
    $('.selectbrand').attr('style','');
    var tebirth = $('select[name="birth-YYYY"]').val()+'-'+$('select[name="birth-MM"]').val()+'-'+$('select[name="birth-DD"]').val();
    var postdata = {
      teid:$('#teid').val(),
      tename:$('#tename').val(),
      tebirth:tebirth,
      tetel:$('#tetel').val(),
      shopname:shopname,
      bname:bname,
      shopid:shopid,
      bid:bid
    }
    $.post('/terminal/updateTerminal', postdata, function(data) {
      if(data==1){
        alert('修改成功');
        window.location.reload();
      }
    });
    return false;
   });
  $('body').on('click', '.updateTerminalCallback', function() {
     var str = '<ul>'+
      '<li><laber class="list-laber">回访内容：</laber><textarea name="shopcallback" class="list-textarea" id="terminalcallback" rows="8" cols="38"></textarea></li>'+
      '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="callbackUpdate">修　改</button></div>'+
      '</ul>';
      var d = dialog({
        title:'修改回访内容',
        content: str,
        width:'450px',
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal(callbackUpdateinit($(this)));
  });
  $('body').on('click', '#callbackUpdate', function() {
    var postdata = {
      id:$(this).val(),
      val:$('#terminalcallback').val()
    }
    $.get('/terminal/updateTerminalcallback', postdata, function(data) {
      if(data!='false'){
        location.reload(true);
      }
    });
  });
  $('body').on('click', '#deleteterminal', function() {
    var content = ''+
                  '<span class="warmin-txt">&nbsp;&nbsp;&nbsp;&nbsp;注意！您正在删除终端！！！删除后与其相关的数据也会一并删除并无法恢复，请先做好备份</span>'+
                  '<div class="btn-box"><button class="btn btn-danger btn-xs" data-teid='+$(this).data('teid')+' id="deleteterminal_go">确定</button> <button class="btn btn-xs" id="closeDelete">取消</button></div>';
    deletedialog = dialog({
      title:'注意',
      content: content,
      width:'400px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $('body').on('click', '#deleteterminal_go', function() {
    $.post('/terminal/terminaldelete', {teid: $(this).data('teid')}, function(data) {
      if(data!='false'){
        alert('删除成功');
        window.location.href = "/terminal/terminallist";
      }else{
        alert('删除失败');
        location.reload(true);
      }
    });
  });
  var callbackUpdateinit = function($this){
    $('#terminalcallback').val($this.parent().prevAll('.J_callback').html());
    $('#callbackUpdate').val($this.data('id'));
  }
  var dialoginit = function($this){
    $('.YMD').ymd();
    var $pare = $this.parent();
    $('#teid').val($('#J_teid').text());
    $('#tename').val($('#J_tename').text());
    $('#bid').text($('#J_bid').text());
    $('#shopid').text($('#J_shopid').text());
    $('#tetel').val($('#J_tetel').text());
    settime($('#J_tebirth').text(),$('#tebirth'));
    $.get('/terminal/getTerinalBrand',{teid:$('#J_teid').text()}, function(data) {
      window.brandcount.splice(0,window.brandcount.length);
      window.brandnamecount.splice(0,window.brandnamecount.length);
      var json = JSON.parse(data);
      window.j=json.length;
      for (var i = 0; i < json.length; i++) {
        window.brandcount[i]=parseInt(json[i].bid);
        window.brandnamecount[i]=json[i].bname;
      }
    });
    $.get('/terminal/getTerinalShop',{teid:$('#J_teid').text()}, function(sdata) {
      window.shopcount.splice(0,window.shopcount.length);
      window.shopnamecount.splice(0,window.shopnamecount.length);
      var json = JSON.parse(sdata);
      window.k=json.length;
      for (var i = 0; i < json.length; i++) {
        window.shopcount[i]=parseInt(json[i].shopid);
        window.shopnamecount[i]=json[i].shopname;
      }
    });
  }
  var settime = function(time,$){
    var time = time.split('-');
    $.children('.YYYY').val(time[0]);
    $.children('.MM').val(parseInt(time[1]));
    writeDay(time,$.children('.DD'));
    $.children('.DD').val(parseInt(time[2]));
  }
	var writeDay = function(time,e){
		var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if(parseInt(time[1])==2 && IsPinYear(parseInt(time[0]))){
      var n = MonHead[1]+1;
    }else{
      var n = MonHead[parseInt(time[1])-1];
    }
		for (var i = 1; i < (n + 1); i++)
			e.append('<option value='+i+'>'+i+'日</option>');
	}
  var IsPinYear = function(year){
    return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0));
  }
}(jQuery);
